﻿using System.IO;

namespace Providers.FileTransfer.Client
{
    public class StreamingCilent
    {
        private readonly FileStream _wrFileStream;
        private static ulong _sLength;
        private static ulong _sReceived;

        public StreamingCilent(string filePath,ulong lenth)
        {
            if (filePath != null)
            {
                Directory.CreateDirectory(Path.GetDirectoryName(filePath));
                _wrFileStream = new FileStream(filePath, FileMode.Create, FileAccess.Write, FileShare.None);
            }
            _sLength = lenth;
        }

        public void Write(byte[] all)
        {
            _sReceived += (ulong)all.Length;
            _wrFileStream.Write(all, 0, all.Length);
        }

        public void End()
        {
            _wrFileStream.Flush();
            _wrFileStream.Close();
            _wrFileStream.Dispose();
            FlagComplete = true;
        }

        public bool FlagComplete { get; set; }

        public int ProgressV
        {
            get
            {
                if (_sReceived >= _sLength)
                {
                    return 100;
                }
                var v = (int)(((float)_sReceived / (float)_sLength) * 100.0f);
                return v;
            }
        }
    }
}
